package com.wzq.controller;

import com.wzq.pojo.Resume;
import com.wzq.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wzq.Jolin
 * @company none
 * @create 2020-08-09 18:59
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {
    public final static String USER_NAME = "admin";
    private final static String USER_PASSWORD = "admin";
    public final static String SESSION_NAME = "USER_SESSION";
    @Value("${server.port}")
    private String port;

    @Autowired
    private ResumeService resumeService;




    @RequestMapping("/add")
    public String add() {
        return "add";
    }


    @RequestMapping("/login")
    public String login() {
        return "index";
    }

    @RequestMapping("/login.do")
    public ModelAndView doLogin(HttpSession httpSession, String username, String password) {
        if (USER_NAME.equals(username) && USER_PASSWORD.equals(password)) {
            httpSession.setAttribute(SESSION_NAME, username);
            return getAllResume(new ModelAndView());
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/");
        modelAndView.addObject("msg", "账号密码错误");
        modelAndView.addObject("status", 0);
        return modelAndView;
    }

    @RequestMapping("/update")
    public ModelAndView update(Long id) {
        Resume resume = resumeService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("update");
        modelAndView.addObject("resume", resume);
        return modelAndView;
    }

    @RequestMapping("/delete")
    public ModelAndView delete(Long id) {
        resumeService.delete(id);
        ModelAndView modelAndView = new ModelAndView();
        return getAllResume(modelAndView);
    }


    @RequestMapping("/add.do")
    public ModelAndView doAdd(String name, String phone, String address) {
        Resume resume = new Resume();
        resume.setName(name);
        resume.setPhone(phone);
        resume.setAddress(address);
        resumeService.add(resume);
        return getAllResume(new ModelAndView());
    }

    @RequestMapping("/update.do")
    public ModelAndView doUpdate(Long id, String name, String phone, String address) {
        Resume resume = new Resume();
        resume.setId(id);
        resume.setName(name);
        resume.setPhone(phone);
        resume.setAddress(address);
        resumeService.update(resume);
        ModelAndView modelAndView = new ModelAndView();
        return getAllResume(modelAndView);
    }

    @RequestMapping("/all")
    public ModelAndView getAllResume(ModelAndView modelAndView) {
        List<Resume> allResume = resumeService.findAllResume();
        modelAndView.addObject("allResumes", allResume);
        modelAndView.addObject("server", port);
        modelAndView.setViewName("list");
        return modelAndView;
    }

}
